package com.xjc.business.controller.schadmin;

import com.alipay.api.internal.util.AlipaySignature;
import com.xjc.business.entity.domain.SchoolSets;
import com.xjc.business.entity.po.schadmin.ActiveSchoolPo;
import com.xjc.business.entity.vo.SetVo2;
import com.xjc.business.entity.vo.SetVo;
import com.xjc.business.entity.vo.SetVo3;
import com.xjc.business.service.SchoolSetsService;
import com.xjc.business.service.SetsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 徐锦程
 * 时间:2025-01-27 9:36
 */
@Controller
@RequestMapping("/sch_admin/set")
public class SchoolSetController {

    @Autowired
    SetsService setsService;

    @Autowired
    SchoolSetsService schoolSetsService;

    /**
     * 获取所有套餐信息
     * @return 套餐信息
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<List<SetVo>> getSetList(){
        return setsService.getSetList();
    }

    /**
     * 获取未支付订单
     * @param id 学校id
     * @return 订单id
     */
    @GetMapping("/query/{id}")
    @ResponseBody
    public Result<Integer> queryNotPayOrder(@PathVariable(value = "id",required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolSetsService.queryNotPayOrder(id);
    }

    /**
     * 删除未支付订单
     * @param id 订单id
     * @return 反馈
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result<String> deleteNotPayOrder(@PathVariable(value = "id",required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return schoolSetsService.deleteNotPayOrder(id);
    }

    /**
     * 开通服务
     * @param activeSchoolPo 学校和套餐信息
     * @return 反馈
     */
    @PostMapping("/active")
    @ResponseBody
    public Result<String> activeSchool(@RequestBody(required = false) ActiveSchoolPo activeSchoolPo){
        if (activeSchoolPo == null || activeSchoolPo.getSchoolId() == null || activeSchoolPo.getSetId() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolSetsService.activeSchool(activeSchoolPo);
    }

    /**
     * 继续支付
     * @param id 订单id
     * @return 反馈
     */
    @PostMapping("/continue/{id}")
    public String continuePay(@PathVariable(value = "id",required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return schoolSetsService.continuePay(id);
    }

    /**
     * 异步支付结果反馈
     * @return success or failure
     */
    @PostMapping("/back/notify_back")
    public void notifyBack(HttpServletRequest request, HttpServletResponse response){

        schoolSetsService.notifyBack(request,response);
    }

    /**
     * 同步支付结果反馈
     */
    @GetMapping("/back/return_back")
    @ResponseBody
    public String returnBack(HttpServletRequest request){

       return schoolSetsService.returnBack(request);
    }

    /**
     * 查询套餐信息
     * @param id 学校id
     * @return 信息
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    public Result<SetVo2> getSetInfo(@PathVariable(value = "id",required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return schoolSetsService.getSetInfo(id);
    }

    /**
     * 获取套餐历史开通信息
     * @param id 学校id
     * @return 信息
     */
    @GetMapping("/history/{id}")
    @ResponseBody
    public Result<List<SetVo3>> getSchoolSetsHistoryList(@PathVariable(value = "id",required = false) Integer id){
        if (id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return schoolSetsService.getSchoolSetsHistoryList(id);
    }
}
